package com.wc.AlgoOJ_train.train2_DP训练.c1.DP0002;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/12 9:38
 * @description 最短路
 * dijkstra
 * http://43.138.190.70:8888/p/DP0002
 */
public class Main {
    static int INF = 1000 * 1000 + 1;
    static int maxN = 1000 + 7;
    static int[][] grip = new int[maxN][maxN];
    static boolean[] flg = new boolean[maxN];
    // 从原点到节点i的最短路径的直接前驱
    static int[] p = new int[maxN];
    static int[] dist = new int[maxN];
    static int n, m;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);


    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            Arrays.fill(grip[i], INF);
        }
        while (m-- > 0) {
            int u = sc.nextInt();
            int v = sc.nextInt();
            int w = sc.nextInt();
            // 只能从小的往大的走
            if (u > v) {
                continue;
            }
            // 只能取最小的
            grip[u][v] = Math.min(grip[u][v], w);
        }
        dijkstra(1);
        out.println(dist[n]);
        out.flush();
    }

    /**
     * @param u 原点
     */
    static void dijkstra(int u) {
        for (int i = 1; i <= n; i++) {
            dist[i] = grip[u][i];
            if (dist[i] == INF) {
                p[i] = -1;
            } else {
                p[i] = u;
            }
        }
        dist[u] = 0;
        flg[u] = true;// 将u加入S集合
        for (int i = 1; i < n; i++) {
            int temp = INF;
            int t = u;
            for (int j = 1; j <= n; j++) {
                // 寻找v - s集合dist的最小值
                if (!flg[j] && dist[j] < temp) {
                    temp = dist[j];
                    t = j;
                }
            }
            if (t == u)
                return;
            flg[t] = true;
            // 松弛,判断v-s中师傅可以借助t更新dist
            for (int j = 1; j <= n; j++) {
                // 更新最短距离
                if (!flg[j] && dist[t] + grip[t][j] < dist[j]) {
                    dist[j] = dist[t] + grip[t][j];
                    // 记录前驱
                    p[j] = t;
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}
